//
//  BECollectionLayoutExtension.swift
//  BasicExtensions
//
//  Created by 范晓鑫 on 2022/10/13.
//

import UIKit

extension UICollectionViewFlowLayout {
    // 设置对齐
    func setAlignment(_ alignment: NSTextAlignment, for axis: NSLayoutConstraint.Axis) {
        let selector = NSSelectorFromString("_setRowAlignmentsOptions:")
        if self.responds(to: selector) {
            switch axis {
            case .horizontal:
                self.perform(selector, with: ["UIFlowLayoutCommonRowHorizontalAlignmentKey": alignment.rawValue,
                                              "UIFlowLayoutLastRowHorizontalAlignmentKey": alignment.rawValue])
            case .vertical:
                self.perform(selector, with: ["UIFlowLayoutRowVerticalAlignmentKey": alignment.rawValue])
            @unknown default: break
            }
        }
    }
}
